package cs340.stormtroopers.junitTests.phase4WhiteBox;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import junit.framework.TestCase;
import cs340.stormtroopers.hypeerweb.HyPeerWeb;
import cs340.stormtroopers.hypeerweb.Node;
import cs340.stormtroopers.hypeerweb.SimplifiedNodeDomain;

/**
 * White box test for Node.constructSimplifiedNodeDomain()
 * Tests to ensure that a SimplifiedNodeDomain object is constructed properly (see the javadoc for
 * the SimplifiedNodeDomain class).
 * @author Daniel Howard
 *
 */

public class ConstructSimplifiedNodeDomainTest extends TestCase
{
	//21, 19, 21, 24, 27, 28, 31 added upPointers but no downPointers
	//20, 22, 23 added downPointers but no upPointers
	//14, 15, 16, 17, 32, 18, 25, 26, 29, 30 did not have upPointers or downPointers
	private HyPeerWeb hypeerweb;
	
	public void setUp(int size)
	{
		hypeerweb = HyPeerWeb.getSingleton();
		
		for(int i = 0; i < size; i++)
		{
			hypeerweb.addAnotherNode();
		}
	}
	
	private void compareHashSetAndArrayList(ArrayList<Node> list, HashSet<Integer> set)
	{
		assertTrue(list.size() == set.size());
		
		for(int i = 0; i < list.size(); i++)
		{
			Node curNeighbor = list.get(i);
			assertTrue(set.contains(curNeighbor.getWebId().getValue()));
		}
	}
	
	private void validateNodeTestResults(int index)
	{
		Node node = hypeerweb.getNode(index);
		SimplifiedNodeDomain testDomain = node.constructSimplifiedNodeDomain();
		
		assertTrue(node.getWebId().getValue() == testDomain.getWebId());
		compareHashSetAndArrayList(node.getNeighbors(), testDomain.getNeighbors());
		compareHashSetAndArrayList(node.getUpPointers(), testDomain.getUpPointers());
		compareHashSetAndArrayList(node.getDownPointers(), testDomain.getDownPointers());
		assertTrue(node.getFold().getWebId().getValue() == testDomain.getFold());
		assertTrue(node.getSurrogateFold().getWebId().getValue() == testDomain.getSurrogateFold());
		assertTrue(node.getInverseSurrogateFold().getWebId().getValue() == testDomain.getInverseSurrogateFold());
	}
	
	private void validateTestResults(int hypeerwebSize)
	{
		for(int i = 0; i < hypeerwebSize; i++)
		{
			validateNodeTestResults(i);
		}
	}
	
	public void test()
	{
		//no neighbors
		//no upPointers and no downPointers
		setUp(1);
		validateTestResults(1);
		
		//1 neighbor
		//no upPointers and no downPointers
		setUp(2);
		validateTestResults(2);
		
		//1 or 2 neighbors
		//1 upPointer and 1 downPointer
		//1 surrogateFold and 1 inverseSurrogateFold
		setUp(3);
		validateTestResults(3);
		
		//Perfect HyPeerCube Test
		//many neighbors
		//no upPointers, no downPointers,
		//no surrogateFolds, no inverseSurrogateFolds
		setUp(1024);
		validateTestResults(1024);
		
		//Imperfect HyPeerWeb Test
		//many neighbors
		//many upPointers and many downPointers
		//many surrogateFolds and many inverseSurrogateFolds
		setUp(769);
		validateTestResults(769);
	}
}
